package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.order.ProductEntity;
import com.hllxd.hism.star.basic.service.service.ProductService;
import com.hllxd.hism.framework.httprpc.service.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * author: xiyong.lxy
 * date: 2020-10-12 16:51
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/console")
public class ConsoleIndexController2 {

    @Autowired
    private ProductService productService;

    @Data
    public static class Params {
        private Integer p1;
        private String p2;
        private String p3;
    }

    public static class ConsoleIndexPage {
        public BacklogStat backlogStat;
        public List<Product> myProducts;
        public CostMonitor costMonitor;
        public List<ProductEntity> hotProducts;
    }
    public static class CostMonitor {
        public Long balance;
        public Long consumeMonth;
        public Long consumeLastDay;
    }

    public static class BacklogStat {
        public Long renew;      // 急需续费
        public Long payoff;     // 待支付
        public Long workorder;  // 待办工单
    }

    public static class Product {
        public Long id;
        public String code;
        public String name;
        public String desc;
        public String icon;
    }

    @RestGrpcMapping("/list.do")
    public NTSTATUS list(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("params: {}", params);
        try {
//            Params aa = params.driverObject(Params.class);
            ConsoleIndexPage page = new ConsoleIndexPage();
            page.backlogStat = new BacklogStat();
            page.myProducts = new ArrayList<>();
            page.costMonitor = new CostMonitor();
            page.hotProducts = productService.hotProduct();

            result.setData(page);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

}
